<template>
  <div>
    <section class="header clearfix">
      <span>缴纳项目</span>
      <span class="iconfont iconadd1" @click="add"> 添加</span>
    </section>
    <div class="search-box" @keyup.enter="searchFun">
      <i class="iconfont iconsearch1"></i>
      <el-input
        placeholder="输入缴纳项目名称进行搜索"
        class="search"
        v-model="searchContent"
      ></el-input>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        tooltip-effect="dark"
        v-loading="loading"
        style="width: 100%"
      >
        <el-table-column prop="paymentId" label="缴纳项目ID"></el-table-column>
        <el-table-column
          prop="paymentName"
          label="缴纳项目名称"
        ></el-table-column>
        <el-table-column
          prop="paymentType"
          label="项目类别"
          :formatter="typeFormatter"
        ></el-table-column>
        <el-table-column label="详情" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="remove(scope.row.paymentId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!--新增对话框-->
    <el-dialog
      title="新增缴纳项目"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :show-close="false"
      center
      width="600px"
    >
      <el-form :model="form">
        <el-form-item label="项目名称">
          <el-input
            v-model="form.paymentName"
            clearable
            placeholder="请输入缴纳项目名称"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="项目类别">
          <el-select v-model="form.paymentType" placeholder="请选择类别">
            <el-option
              v-for="item in itemClassification"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogFormVisible = false;
            addLoading = false;
          "
          >取 消</el-button
        >
        <el-button type="primary" :loading="addLoading" @click="confirmAdd()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!--修改对话框-->
    <el-dialog
      title="编辑缴纳项目"
      :visible.sync="dialogEdit"
      :close-on-click-modal="false"
      :show-close="false"
      center
      width="600px"
    >
      <el-form :model="form">
        <el-form-item label="项目名称">
          <el-input
            v-model="form.paymentName"
            clearable
            placeholder="请输入缴纳项目名称"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="项目类别">
          <el-select v-model="form.paymentType" placeholder="请选择类别">
            <el-option
              v-for="item in itemClassification"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogEdit = false;
            editLoading = false;
          "
          >取 消</el-button
        >
        <el-button
          type="primary"
          :loading="editLoading"
          @click="confirmUpdate()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template> 

<script>
import { requestApiPost } from "../api/api";
import { reqRep } from "../report/reqReport";
import { resRep } from "../report/resReport";
import commonData from "@/assets/js/commonData";
export default {
  name: "payTheProject",
  data() {
    return {
      loading: false,
      dialogFormVisible: false,
      addLoading: false,
      editLoading: false,
      dialogEdit: false,
      form: {
        paymentId: "",
        paymentName: "",
        paymentType: "",
      },
      formLabelWidth: "160px",
      searchContent: "",
      tableData: [],
      current: 1,
      pageSize: 6,
      total: null,
      itemClassification: [],
    };
  },
  methods: {
    typeFormatter(a, b, c) {
      return this.itemClassification[c].label;
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getData();
    },
    handleCurrentChange(e) {
      this.current = e;
      this.getData();
    },
    //删除项目
    remove(paymentId) {
      this.$confirm("确定删除该项目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.removeProject(paymentId);
        })
        .catch(() => {});
    },
    removeProject(paymentId) {
      var reqData = {
        paymentId: paymentId,
      };
      let reqReport = reqRep({ traCode: "_DEL_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        }
      });
    },
    //添加
    add() {
      this.$set(this.form, "paymentName", "");
      this.$set(this.form, "paymentType", "");
      this.dialogFormVisible = true;
    },
    confirmAdd() {
      if (!this.form.paymentName) {
        this.$message.warning("请输入项目名称");
        return;
      }
      if (!this.form.paymentType) {
        this.$message.warning("请选择类别");
        return;
      }
      var reqData = {
        paymentName: this.form.paymentName,
        paymentType: this.form.paymentType,
      };
      this.addLoading = true;
      let reqReport = reqRep({ traCode: "_ADD_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.addLoading = false;
        } else {
          this.dialogFormVisible = false;
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.addLoading = false;
          this.getData();
        }
      });
    },
    //修改
    edit(e) {
      this.$set(this.form, "paymentId", e.paymentId);
      this.$set(this.form, "paymentName", e.paymentName);
      this.$set(this.form, "paymentType", e.paymentType);
      this.dialogEdit = true;
    },
    confirmUpdate() {
      if (!this.form.paymentName) {
        this.$message.warning("请输入项目名称");
        return;
      }
      if (!this.form.paymentType) {
        this.$message.warning("请选择类别");
        return;
      }
      var reqData = {
        paymentId: this.form.paymentId,
        paymentName: this.form.paymentName,
        paymentType: this.form.paymentType,
      };
      this.editLoading = true;
      let reqReport = reqRep({ traCode: "_EDIT_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
          this.editLoading = false;
        } else {
          this.dialogEdit = false;
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.editLoading = false;
          this.getData();
        }
      });
    },

    searchFun() {
      this.current = 1;
      this.tableData = [];
      this.getData();
    },
    //获取列表
    getData() {
      this.loading = true;
      var reqData = {
        paymentName: this.searchContent,
        current: this.current,
        pageSize: this.pageSize,
      };
      let reqReport = reqRep({ traCode: "_LIST_PAYMENT", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.tableData = resReport.data.records;
          this.total = parseInt(resReport.data.total);
        }
      });
    },
  },
  created() {
    this.itemClassification = commonData.itemClassification;
    this.getData();
  },
  mounted() {},
};
</script>
<style scoped>
.header {
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  font-size: 14px;
}
.header .org {
  float: right;
  cursor: pointer;
}
.org:hover {
  color: #13cb6e;
}
.header i {
  font-size: 20px;
  margin-right: 5px;
}
.opt-user {
  padding: 20px 15px;
}
.opt-user > section {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.opt-user i {
  font-size: 20px;
  margin-right: 5px;
  vertical-align: middle;
}
.iconadd1 {
  float: right;
  font-size: 14px;
  color: #13cb6e;
  cursor: pointer;
}
.search-box {
  height: 50px;
  line-height: 50px;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
}
.search-box i {
  margin-left: 15px;
  vertical-align: middle;
}
.search-box .search {
  display: inline-block;
  width: calc(100% - 36px);
}
.search-box .option {
  display: inline-block;
  margin-left: 15px;
}
.search-box .el-select {
  width: 150px;
}
.search-box .el-select input {
  min-width: 100px;
  background-color: #eee;
}
.search-box .batchChange {
  display: inline-block;
  color: #999;
  font-size: 14px;
  cursor: pointer;
}
i {
  font-size: 16px;
  color: #999;
}
.el-form-item {
  text-align: center !important;
}
.el-form-item:first-child {
  margin-bottom: 22px !important;
}
</style>
